package com.ruijie.acl.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import com.ruijie.acl.AclPlanParameters;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.service.AclDeviceService;
import com.ruijie.acl.service.AclPlanParametersService;
import com.ruijie.acl.service.DeviceTimeRangeService;
import com.ruijie.acl.service.TimeRangeService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.TimeRangeInfo;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.web.config.Constants;
import com.ruijie.batch.core.factory.SpringConfigAware;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.batch.plan.items.ItemParameters;
import com.ruijie.batch.task.ItemProcessResult;
import com.ruijie.emp.batch.task.AbstractDeviceExtendTasklet;
import com.ruijie.emp.device.Device;
import com.ruijie.emp.system.service.TelnetTemplateService;
import com.ruijie.foundation.exception.BusinessException;

/**
 * 
 * <p>
 * Title: 重新部署时间段计划
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-27
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class ReDeployTimeRange extends AbstractDeviceExtendTasklet implements SpringConfigAware {

	private Log logger = LogFactory.getLog(ReDeployTimeRange.class);

	private TelnetTemplateService service;

	private AclPlanParametersService aclPlanParametersService;

	private TimeRangeService timeRangeService;

	private TaskConfigAclDevice taskConfigAclDevice;
	
	private DeviceTimeRangeService deviceTimeRangeService;
	
	private AclDeviceService aclDeviceService;

	private Map<String, String> paramMap = new HashMap<String, String>();

	protected Map<String, String> parseParameters(ItemParameters itemParameters) {
		return paramMap;
	}

	@SuppressWarnings("unchecked")
	public ItemProcessResult processItem(Object item, ItemParameters itemParameters) {
		ItemProcessResult result = new ItemProcessResult();
		paramMap.clear();
		Assert.notNull(item, "item is null");

		PlanParameters planParameters = new PlanParameters();
		AclPlanParameters aclPlanParamters = null;
		try{
			String aclPlanParamtersId = itemParameters.getParameterValue(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME);
			aclPlanParamters = aclPlanParametersService.get(Long.parseLong(aclPlanParamtersId));
		}catch(Exception ex){
			throw new BusinessException("acl_deployment_initparam_error");
		}
		List<TimeRange> timeRangeList = null;
		
		// 转换时间段信息
		if (!AclUtil.empty(aclPlanParamters.getTimeRanges())) {
			timeRangeList = timeRangeService.findByIds(aclPlanParamters.getTimeRanges());
			if (timeRangeList != null && timeRangeList.size() > 0){
				List<TimeRangeInfo> timeRangeInfoList = AclConfigUtil.timeRangeList2TimeRangeInfoList(timeRangeList);
				this.paramMap.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_TIMENAMES, AclConfigUtil
						.convertTimeRangeNames(timeRangeInfoList));
				planParameters.putParameter("timerangeInfoList", timeRangeInfoList);
				// 调用下发
				planParameters.putParameter("dealFlag", "modify");
				
				try {
					
					result = taskConfigAclDevice.processItem(item, planParameters);
					for(TimeRange timeRange:timeRangeList){
						timeRangeService.saveSameWithAclDevice(timeRange,aclDeviceService.getByDevice((Device)item),null);
						deviceTimeRangeService.updateChangedByTimeRange(timeRange,false,aclDeviceService.getByDevice((Device)item));
						if(!deviceTimeRangeService.existByTimeRangeAndChanged(timeRange)){
							timeRangeService.refresh(timeRange);
							timeRange.setIsChanged(false);
							timeRangeService.update(timeRange);
						}
					}
				} catch (BusinessException e) {
					if (timeRangeList != null && timeRangeList.size() > 0){
						for(TimeRange timeRange:timeRangeList){
							try {
								if(deviceTimeRangeService.existByTimeRangeAndChanged(timeRange)){
									timeRangeService.refresh(timeRange);
									timeRange.setIsChanged(true);
									timeRangeService.update(timeRange);
								}
							} catch (Exception ex) {
								//
							}	
						}
					}
					throw e;
				}
				return result;
			}
		} else {
			logger.warn("要部署的时间段信息列表为空！");
		}

		return result;
	}

	public void setService(TelnetTemplateService service) {
		this.service = service;
	}

	public AclPlanParametersService getAclPlanParametersService() {
		return aclPlanParametersService;
	}

	public void setAclPlanParametersService(AclPlanParametersService aclPlanParametersService) {
		this.aclPlanParametersService = aclPlanParametersService;
	}

	public TimeRangeService getTimeRangeService() {
		return timeRangeService;
	}

	public void setTimeRangeService(TimeRangeService timeRangeService) {
		this.timeRangeService = timeRangeService;
	}

	public TaskConfigAclDevice getTaskConfigAclDevice() {
		return taskConfigAclDevice;
	}

	public void setTaskConfigAclDevice(TaskConfigAclDevice taskConfigAclDevice) {
		this.taskConfigAclDevice = taskConfigAclDevice;
	}

	public TelnetTemplateService getService() {
		return service;
	}

	public DeviceTimeRangeService getDeviceTimeRangeService() {
		return deviceTimeRangeService;
	}

	public void setDeviceTimeRangeService(
			DeviceTimeRangeService deviceTimeRangeService) {
		this.deviceTimeRangeService = deviceTimeRangeService;
	}

	public AclDeviceService getAclDeviceService() {
		return aclDeviceService;
	}

	public void setAclDeviceService(AclDeviceService aclDeviceService) {
		this.aclDeviceService = aclDeviceService;
	}

}
